package actions;

import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.StudentDBModel;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import data.*;

public class Chairs_4_toApps extends Action{
	
	StudentDBModel model = new StudentDBModel();
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		
		StudentDBModel model = new StudentDBModel();		
		String reviewer = "";
		Vector<String> appsInDisc;
		Vector<String> appsInSpec;
		Vector<ReviewerInfo> reviewerInfos;
		Vector<String> reviewers;
		
		if(request.getAttribute("reviewerInfos") != null){
			reviewerInfos = (Vector<ReviewerInfo>)request.getAttribute("reviewerInfos");
		}
		else{
			reviewerInfos = (Vector<ReviewerInfo>)request.getSession().getAttribute("reviewerInfos");
		}
		if(request.getAttribute("reviewers") != null){
			reviewers = (Vector<String>)request.getAttribute("reviewers");
		}
		else{
			reviewers = (Vector<String>)request.getSession().getAttribute("reviewers");
		}
		
		reviewers = StudentDBModel.getReviewers();

for(int i4 = 0; i4 < reviewerInfos.size(); i4++){
	System.err.println("Chairs_4_toApps");
System.err.println("reviewerInfos("+i4+") name= "+reviewerInfos.elementAt(i4).getName() );
for(int i5 =0; i5<reviewerInfos.elementAt(i4).getGradedApps().size(); i5++){
System.err.println("gradedApps("+i5+") score= "+reviewerInfos.elementAt(i4).getGradedApps().elementAt(i5).getApplicantName() );
}
for(int i6 =0; i6<reviewerInfos.elementAt(i4).getUnGradedApps().size(); i6++){
System.err.println("gradedApp("+i6+") status= "+reviewerInfos.elementAt(i4).getUnGradedApps().elementAt(i6).getApplicantName() );
	}

}			


		
		//MAPPING 1
		if(mapping.getPath().equals("/pages/chairs/GradedVNon(a)") || mapping.getPath().equals("/pages/chairs/GradedVNon(b)") ){
			
			if(request.getSession().getAttribute("appsParam") != null ){
				request.setAttribute("appsParam", (String)request.getSession().getAttribute("appsParam") );
System.err.println("(42nd)appsParam: "+request.getSession().getAttribute("appsParam"));
			}
			else{
				request.setAttribute("appsParam", (String)request.getParameter("appsParam") );
System.err.println("(41st)appsParam: "+request.getAttribute("appsParam"));
			}
			if(request.getSession().getAttribute("reviewer") != null){
				request.setAttribute("reviewer", (String)request.getSession().getAttribute("reviewer") );
			}
			else{
				request.setAttribute("reviewer", (String)request.getParameter("reviewer"));
			}
			
			if(request.getParameter("button") != null)
			{								
				request.setAttribute("reviewer", (String)request.getAttribute("reviewer") );	
				model.updateStatus((String)request.getAttribute("reviewer"), request.getParameter("button") );
				//TODO: update the reviewerInfos to contain the proper student applicaiton status!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
			}
			else{
				request.setAttribute("reviewer", request.getParameter("reviewer") );
			}
		}
		
		//MAPPING 2
		if(mapping.getPath().equals("/pages/chairs/ApplicationsFromDisc")){		
			
			appsInDisc = model.getAppsInDisc(request.getParameter("discNum"));	
			/*for(int i=0; i< appsInDisc.size(); i++){
			System.err.println(appsInDisc.elementAt(i));
		}*/
			request.setAttribute("appsInDisc", appsInDisc);
			request.setAttribute("appsParam", request.getParameter("appsParam") );
			
		}
		
		//MAPPING 3
		if(mapping.getPath().equals("/pages/chairs/ApplicationsFromSpec")){
			
			appsInSpec = model.getAppsInSpec(request.getParameter("specNum"));
			/*for(int i=0; i< appsInSpec.size(); i++){
				System.err.println(appsInSpec.elementAt(i));
			}*/
			request.setAttribute("appsInSpec", appsInSpec);
			request.setAttribute("appsParam", request.getParameter("appsParam") );
		}
		
		//MAPPING 4
		if(mapping.getPath().equals("/pages/chairs/ajaxApplicaiton")){
			
			appsInSpec = model.getAppsInSpec(request.getParameter("specNum"));

			request.setAttribute("ajaxInfo", model.getAjaxInfo((String)request.getSession().getAttribute("student") )  );
			request.setAttribute("appsParam", request.getParameter("appsParam") );
		}
	

		
		request.setAttribute("reviewers", reviewers);
		request.setAttribute("reviewerInfos", reviewerInfos);
		
		return mapping.findForward("success");
	}

}
